//
//  utilities.c
//  Dominion
//
//  Created by Eric Hook on 2/13/14.
//  Copyright (c) 2014 Eric Hook. All rights reserved.
//

#include "headers.h"


int getTreasureDeckCount (struct gameState *G, int player){   // check to see how many Treasure cards in deck
    int TreasureCount = 0;
    for (int j = 0; j < G->deckCount[player]; j++) {
        if ( (G->deck[player][j] == (copper)) || (G->deck[player][j] == (silver)) || (G->deck[player][j] == (gold)) ) {
            TreasureCount++;
        }
    }
    if (DEBUG) printf("Player %i getTreasureDeckCount() count is: %i. Cards in deck: %i \n", player, TreasureCount, G->deckCount[player]);
    return TreasureCount;
}

int getTreasureHandCount (struct gameState *gs, int player){   // check to see how many Treasure cards in deck
    int TreasureCount = 0;
    for (int j = 0; j < gs->handCount[player]; j++) {
        if ( (gs->hand[player][j] == (copper)) || (gs->hand[player][j] == (silver)) || (gs->hand[player][j] == (gold)) ) {
            TreasureCount++;
        }
    }
    if (DEBUG) printf("Player %i TreasureHandCount count is: %i. Cards in deck: %i\n", player, TreasureCount, gs->deckCount[player]);
    return TreasureCount;
}

int compareTreasureDeckCounts (struct gameState *gspost, struct gameState *gspre) {    //  make sure the copper count before and after card played is correct
    for (int i = 0; i < gspost->numPlayers; i++) {
        int preTreasure =  getTreasureDeckCount(gspre, i);
        if (preTreasure >= 2){                                                       // subtract 2 copper IF non-turn player has 2 or more copper in hand
            if (DEBUG) printf("**** POSSIBLE BUG >> if more than 2 treasure cards in deck, player should move 2 treasure cards from deck to hand. deckCount DOES decrement, but the prior values still in array. POSSIBLE bug depending upon interaction with other cards. \n");
            //assert (postTreasure == preTreasure + 2);                         // player should have 2 less copper after card played
            //assert (Gpre->deckCount[G->whoseTurn] == Gpost->deckCount[G->whoseTurn] + 2);                    // indicates two cards removed from deck
            //assert (Gpre->handCount[G->whoseTurn] + 2 == Gpost->handCount[G->whoseTurn]);                    // indicates two cards removed from deck
        }
        if (preTreasure < 2){
            if (DEBUG) printf("**** POSSIBLE BUG >> if more than 2 treasure cards in deck, player should move 2 treasure cards from deck to hand. deckCount DOES decrement, but the prior values still in array. POSSIBLE bug depending upon interaction with other cards. \n");
            //assert ( postTreasure == 0 );                                        // player should have ZERO copper after card played
        }
    }
    return 0;
}

int compareCoins (struct gameState *gspost, struct gameState *gspre){
    //assert (gspost->coins = gspre->coins + 2);   // two coins should be award for cutpurse card
    return 0;   // 0 if true
}

int compareNumActions ( struct gameState *gspost, struct gameState *gspre ){
    return ( gspost->numActions - gspre->numActions );
}

int compareHandCards ( struct gameState *gspost, struct gameState *gspre ){
    return ( gspost->handCount[whoseTurn(gspost)] - gspre->handCount[whoseTurn(gspost)]);
}

int getRandomKingdomCard (int kindomCardSet[]){
    int numCardsInSet = ((sizeof(*kindomCardSet) / (sizeof(int))));
    return rand()%numCardsInSet;
}

int getRandomTreasureCard (){
    int treasureCards[] = {copper, silver, gold};
    return treasureCards[rand()%3];
}

int getCopperCount (struct gameState *gs, int player){
    int copperCount = 0;
    for (int j = 0; j < gs->handCount[player]; j++) {
        if (gs->hand[player][j] == copper) {
            copperCount++;
        }
    }
    if (DEBUG) printf("Player %i copper count is: %i.\n", player, copperCount);
    return copperCount;
}

int compareCopperCounts (struct gameState *gspre, struct gameState *gspost) {     //  make sure the copper count before and after card played is correct
    for (int i = 0; i < gspost->numPlayers; i++) {
        int preCopper = getCopperCount(gspre, i);
        int postCopper = getCopperCount(gspost, i);
        if (whoseTurn(gspost) == i) {
            if (DEBUG) printf("    TURN player %i > pre-copper: %i, post-copper %i *** turn player should lose 0 copper in hand!\n", i, preCopper, postCopper);
            //assert ( postCopper == preCopper);           // current player should not lose copper
        }
        if (whoseTurn(gspost) != i) {
            if (postCopper >= 2){                                               // subtract 2 copper IF non-turn player has 2 or more copper in hand
                if (DEBUG) printf("NON-TURN player %i > pre-copper: %i, post-copper %i *** non-turn player should lose 2 not 1 copper if 2+ copper in hand!\n", i, preCopper, postCopper);
                //assert (postCopper == preCopper+2);                           // player should have 2 less copper after card played
            }
            if (postCopper < 2){
                if (DEBUG) printf("NON-TURN player %i > pre-copper: %i, post-copper %i  *** non-turn player should lose 2 not 1 copper if 2+ copper in hand!\n", i, preCopper, postCopper);
                //assert ( preCopper == 0 );                                    // player should have ZERO copper after card played
            }
        }
    }
    return 0;
}

